package com.lby.redisdemo.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.IdUtil;
import com.lby.redisdemo.config.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.Cache;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName TestController
 * @Description TODO
 * @Author louby0923
 * @Date 2023/3/29 10:48
 * @Version 1.0
 */
@Slf4j
@RestController
public class TestController {
    @Autowired
    RedisUtil redisUtil;
    final String key = "lock";


    @RequestMapping("/test")
    public String myLock() {
        String content = "";
        String uid = IdUtil.fastUUID();
        boolean s = redisUtil.tryLock(key, uid);
        if (s) {
            ThreadUtil.sleep(3, TimeUnit.SECONDS);
            System.out.println(redisUtil.get(key));
            redisUtil.unLock(key, uid);
            content = DateTime.now() + "->我获取到了锁";
            log.error(content);
        } else {
            content = DateTime.now() + "未能获取到锁";
        }


        return content;
    }



}
